package com.dzl.config.exception;

import com.dzl.util.DingTalkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.dzl.util.HttpCode;
import com.dzl.util.JsonResult;
import com.github.pagehelper.PageException;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 全局异常捕获处理
 * @date 2020/11/24 16:34
 * @author lishunxing
 */
@RestControllerAdvice
@Slf4j
public class GlobalDefaultExceptionHandler {

	/**
	 * 通用异常拦截
	 * @date 2020/11/24 16:36
	 * @author lishunxing
	 * @param ex 异常类
	 * @return com.dzl.util.JsonResult
	 */
	/*@ExceptionHandler(value = CommonsException.class)
	public JsonResult interceptorCommonsException(CommonsException ex){
		ex.printStackTrace();
		return JsonResult.failWithMsg(ex.getMessage());
	}*/


	/**
	*@Description:分页异常拦截
	*@param ex 分页异常类
	*@return
	*JsonResult
	*@exception:
	*@author:pengkailun
	*@time:2021年3月26日下午4:33:56
	 */
	@ExceptionHandler(value = PageException.class)
	public JsonResult pageException(PageException ex){
		return JsonResult.failWithMsg(ex.getMessage());
	}



	/**
	*@Description:请求方式异常拦截
	*@param ex  请求方式异常类
	*@return
	*JsonResult
	*@exception:
	*@author:pengkailun
	*@time:2021年3月26日下午4:36:20
	 */
	@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
	public JsonResult httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex){
		return JsonResult.failWithMsg("请求方法错误，请核对文档，是GET或POST请求!");
	}


	/**
	 * 登录异常拦截
	 * @date 2020/11/24 16:36
	 * @author lishunxing
	 * @param ex 异常类
	 * @return com.dzl.util.JsonResult
	 */
	/*@ExceptionHandler(value = TokenException.class)
	public JsonResult interceptorTokenException(TokenException ex){
		return new JsonResult<>(null,HttpCode.TOKEN_INVALID);
	}
*/
	/**
	 * 删除异常拦截
	 * @date 2020/11/24 16:36
	 * @author lishunxing
	 * @param ex 异常类
	 * @return com.dzl.util.JsonResult
	 */
	@ExceptionHandler(value = DeleteException.class)
	public JsonResult interceptorDeleteException(DeleteException ex){
		return new JsonResult(ex.getMessage(),HttpCode.DELETE_ERR);
	}
	/**
	 *
	 * @Description:除去通用异常、登录异常其他的异常拦截
	 * @param ex
	 * @return
	 * JsonResult
	 * @exception:
	 * @author: liufan
	 * @time:2021年2月23日 下午2:08:05
	 */
	@ExceptionHandler(value = Exception.class)
	public JsonResult defaultErrorHandler(Exception ex,HttpServletRequest request) {
		ex.printStackTrace();
//		StringWriter sw = new StringWriter();
//		try (PrintWriter pw = new PrintWriter(sw)) {
//			ex.printStackTrace(pw);
//		}
//		DingTalkUtil.postWithJson(String.format("\n接口URL：%s \n %s", request.getRequestURI(),sw));
		DingTalkUtil.postWithJson(String.format("\n接口URL：%s \n %s", request.getRequestURI(),ex.getMessage()));
		log.error(ex.getMessage());
		return new JsonResult<>(null,HttpCode.ERROR);
	}

    @ExceptionHandler(CommonException.class)
    public <T> JsonResult<T> commonException(CommonException e) {
        log.error("业务异常 message={}", e.getMessage());
        return JsonResult.fail(e.getCode(), e.getMessage());
    }
}
